package top.dancun.easbaseinfo.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.dancun.common.utils.DataUtils;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;
import top.dancun.easbaseinfo.entity.ClassEntity;
import top.dancun.easbaseinfo.entity.MajorEntity;
import top.dancun.easbaseinfo.service.ClassService;
import top.dancun.easbaseinfo.service.DeptService;
import top.dancun.easbaseinfo.service.MajorService;

import java.lang.reflect.Field;
import java.util.*;


/**
 * 
 *
 * @author dancun
 * @email message@dancun.top
 * @date 2022-09-17 16:31:58
 */
@RestController
@RequestMapping("class")
public class ClassController extends AbstractController{
    @Autowired
    private ClassService classService;

    @Autowired
    private MajorService majorService;

    @Autowired
    private DeptService deptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("easbaseinfo:class:list")
    public R list(@RequestBody Map<String, Object> params){
        //TODO 条件查询 年份模糊查询

        Map<String,String> datamap = DataUtils.mapAddNoEmpty(params,ClassEntity.class);
        PageUtils page = classService.queryPage(params,datamap);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listnopage")
    @RequiresPermissions("easbaseinfo:class:list")
    public R listNoPage(@RequestBody Map<String, Object> params){
        //TODO 条件查询 年份模糊查询

        Map<String,String> datamap = DataUtils.mapAddNoEmpty(params,ClassEntity.class);
        List<ClassEntity>  data= classService.query(datamap);

        return R.ok().put("data", data);

//        List<ClassEntity> classEntityList = classService.list(params);
//        return R.ok().put("data", classEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/getbyids")
    @RequiresPermissions("easbaseinfo:class:list")
    public R getByIds(@RequestBody List<Integer> ids){
        //TODO 条件查询 年份模糊查询



        List<ClassEntity>  data= classService.queryByIds(ids);

        return R.ok().put("data", data);

//        List<ClassEntity> classEntityList = classService.list(params);
//        return R.ok().put("data", classEntityList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{classId}")
//    @RequiresPermissions("easbaseinfo:class:info")
    public R info(@PathVariable("classId") Integer classId){
		ClassEntity classEntity = classService.getById(classId);
        MajorEntity major = majorService.getById(classEntity.getMajorId());
        if(Objects.nonNull(major)){
            //查询相关学院和专业
            classEntity.setMajor(major);
            classEntity.setDept(deptService.getById(major.getDeptId()));
        }

        return R.ok().put("class", classEntity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("easbaseinfo:class:save")
    public R save(@RequestBody ClassEntity classEntity){
		classService.save(classEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("easbaseinfo:class:update")
    public R update(@RequestBody ClassEntity classEntity){
		classService.updateById(classEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("easbaseinfo:class:delete")
    public R delete(@RequestBody Integer[] classIds){
		classService.removeByIds(Arrays.asList(classIds));

        return R.ok();
    }

}
